package 二叉树相关问题.中度题;

public class 求路径和 {

    public int pathSum(TreeNode root, int sum) {
        if (root==null)
            return 0;
        return traverse(root,sum)+pathSum(root.left,sum)+pathSum(root.right,sum);
    }

    private int traverse(TreeNode root, int sum) {
        if (root==null)
            return 0;
        sum-=root.val;
        int count= sum==0?1:0;
        count+=traverse(root.left,sum);
        count+=traverse(root.right,sum);
        return count;
    }
}
